package hook

import (
	"bytes"
	"fmt"
	"gopkg.in/gomail.v2"
	"strings"
)

type Email struct {
	*gomail.Dialer
	UserName string
}

// 新的邮箱客户端
func NewEmailCli(c *EmailConf)*Email{
	return &Email{
		Dialer: gomail.NewDialer(c.Host,c.Port, c.Addr, c.Pass),
		UserName: c.UserName,
	}
}

// 推送消息的邮件
func (e *Email)PushEventSend(push *Push)(err error){
	if len(push.config.Event.Push.EmailRecipient) == 0 {
		return nil
	}

	msg := gomail.NewMessage()
	// 发件人
	msg.SetAddressHeader("From", e.Username, e.UserName)
	// 收件人
	for _,user := range push.config.Event.Push.EmailRecipient{
		msg.SetHeader("To", msg.FormatAddress(user.Addr, user.UserName))
	}
	// 主题
	msg.SetHeader("Subject", push.config.Event.Push.EmailTitle) //设置邮件主题
	// 内容
	msg.SetBody("text/html", e.makePushBody(push))


	if err = e.DialAndSend(msg); err != nil{
		return fmt.Errorf("邮件发送失败:%w", err)
	}

	return nil
}

func (e *Email)makePushBody(p *Push)string{
	body := `
<h2 style="font-weight: 700;border-bottom: 1px solid #eee;padding-bottom: 4px;font-size: 1.3em;margin: 23px 0 18px 0;">基础信息</h2>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">推送人: <snap style="color:#d9534f">`+p.HeadCommit.Author.Name+`</snap></p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">提交地址: `+p.HeadCommit.URL+`</p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">上一条 commit id: `+p.Before+`</p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">当前 commit id: `+p.After+`</p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">对比地址: `+p.Compare+`</p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">提交描述: `+p.HeadCommit.Message+`</p>
<p style="margin-top: 0;margin-bottom: 10px;line-height: 30px;">提交时间: `+p.HeadCommit.Timestamp+`</p>

<h2 style="font-weight: 700;border-bottom: 1px solid #eee;padding-bottom: 4px;font-size: 1.3em;margin: 23px 0 18px 0;">更改文件</h2>
<ol>{{modified}}</ol>

<h2 style="font-weight: 700;border-bottom: 1px solid #eee;padding-bottom: 4px;font-size: 1.3em;margin: 23px 0 18px 0;">备注</h2>
{{remark}}

<h2 style="font-weight: 700;border-bottom: 1px solid #eee;padding-bottom: 4px;font-size: 1.3em;margin: 23px 0 18px 0;">通知成员</h2>
<ol>{{users}}</ol>

<p style="color:#fafafa;font-size:12px;">技术支持: <a href="http://mnyuan.com" style="color:#fafafa;font-size:12px;text-decoration: none;">大王(慢鸟)</a></p>
`
	modified,remark,users := "", "<p></p>",""

	// 填充修改文件
	// 添加
	for _,diff := range p.HeadCommit.Added{
		modified += `<li><snap style="background-color:#5cb85c;padding:.2em.6em.3em;font-size:60%;font-weight:700;line-height:1;color:#fff;border-radius:.25em;">添加</snap> `+diff+`</li>`
	}
	// 修改
	for _,diff := range p.HeadCommit.Modified{
		modified += `<li><snap style="background-color:#5bc0de;padding:.2em.6em.3em;font-size:60%;font-weight:700;line-height:1;color:#fff;border-radius:.25em;">修改</snap> `+diff+`</li>`
	}
	// 删除
	for _,diff := range p.HeadCommit.Removed{
		modified += `<li><snap style="background-color:#777;padding:.2em.6em.3em;font-size:60%;font-weight:700;line-height:1;color:#fff;border-radius:.25em;">删除</snap> `+diff+`</li>`
	}
	body = strings.Replace(body, "{{modified}}", modified, 1)

	// 填充sell结果
	if sl := p.GetSellResult(); sl != nil {
		if bytes.HasPrefix(sl, []byte("error:"))  {
			remark = `<b style="color: #a94442;">Sell 结果:</h4><pre style="padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;word-break: break-all;word-wrap: break-word;background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 4px;">`+string(sl)+`</pre>`
		}else {
			remark = `<h4>Sell 结果:</h4><pre style="padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;word-break: break-all;word-wrap: break-word;background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 4px;">`+string(sl)+`</pre>`
		}
	}
	body = strings.Replace(body, "{{remark}}", remark, 1)

	// 填充推送成员
	for _,user := range p.config.Event.Push.EmailRecipient{
		users += `<li>`+user.UserName+` - `+user.Addr+`</li>`
	}

	body = strings.Replace(body, "{{users}}", users, 1)
	//fmt.Println("email body:", body)

	return body
}

// 拉取的邮件通知发送
func (e *Email)PullEventSend(){

}